<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<div class="topBarR">
	<form action="#" method="post">
		<input type="text" class="search" required="" placeholder="请输入搜索的内容..." target="_blank">
	</form>
	<ul id="login_ui">
		<c:if test="${sessionScope.user == null}">
			<li class="tologin">
				<a class="logina" href="javascript:" id="tologin" >登录</a>
			</li>
			<li>
				<a class="user_login" style="padding: 1px 0 0 0; margin: 0;">|</a>
			</li>
			<li class="toregist">
				<a class="" id="toregist" href="javascript:">注册</a>
			</li>
		</c:if>
	
		<c:if test="${sessionScope.user != null}">
			<li class="tologin">
				<input type="hidden" id="user_id" value="${user.id}"/>
				<a class="logina" href="${pageContext.request.contextPath }/user/myspace"  id="user_select" style="color:red;">${user.username}</a>
			</li>
			<li>
				<a class="user_login" style="padding: 1px 0 0 0; margin: 0;">|</a>
			</li>
			<li class="toregist">
				<a class="" href="javascript:" id="loginout">注销</a>
			</li>
		</c:if>
	</ul>
</div>

<link rel="stylesheet" href="${pageContext.request.contextPath }/css/base.css" />
<link rel="stylesheet" href="${pageContext.request.contextPath }/css/global.css" />
<link rel="stylesheet" href="${pageContext.request.contextPath }/css/login-register.css" />

<style>
	#registform h4{height:35px;line-height:35px;overflow:hidden;background:#00AA98;color:#fff;padding:0 10px;border:1px solid #00AA98;font-size:14px;border-bottom:1px solid #00AA98;}
	#registform h4 a{width:18px;line-height:18px;_line-height:18px;height:18px;font-family:arial;overflow:hidden;display:block;background:#fff;color:#c77405;float:right;text-align:center;text-decoration:none;margin-top:7px;font-size:20px;font-weight:normal;border-radius:2px;_font-size:12px;}
</style>

<div class="login-form fr" style="display: none;" id="registform">
	<h4><a href="javascript:void(0)" onclick="closeThis()" title="关闭窗口">×</a>注册</h4>
	<div class="form-hd">
		<h3>用户注册</h3>
	</div>
	<div class="form-bd">
			<dl>
				<dt>用户名</dt>
				<dd><input type="text" name="tusername" id="tusername" class="text" /></dd>
			</dl>
			<dl>
				<dt>密码</dt>
				<dd><input type="password" name="tuserpass" id="tuserpass" class="text"/></dd>
			</dl>
			<dl>
				<dt>确认密码</dt>
				<dd><input type="password" name="tuserpass2" id="tuserpass2" class="text"/></dd>
			</dl>
			<dl>
				<dt>邮箱</dt>
				<dd><input type="text" name="temail" id="temail" class="text"/></dd>
			</dl>
			<dl>
				<dt>验证码</dt>
				<dd><input type="text" id="tyanzheng" class="text" size="10" style="width:58px;">
					<img src="/dogstore/imgcode.action" alt="" align="absmiddle" onclick="changeImg(this)" style="position:relative;top:-2px;"/> 
					<a href="javascript:"  style="color:#999;">点击图片切换</a>
				</dd>
			</dl>
			<dl>
				<dt>&nbsp;</dt>
				<dd><input type="button" id="regist" value="立即注册" class="layui-btn" value="立即注册"/>
					<input type="checkbox" checked="checked"/>阅读并同意<a href="" class="forget">服务协议</a>
				</dd>
			</dl>
		
	</div>	
</div>

<script type="text/javascript" src="${pageContext.request.contextPath }/js/easydialog.min.js"></script>
<script>
//弹出批量操作dialog
$("#toregist").click(function (){
	easyDialog.open({
		container : 'registform',
		overlay : true
	});
});

function closeThis(){
	easyDialog.close();
}

//点击注册
$("#regist").click(function (){
	//alert("regist");
	var username = $("#tusername").val();
	var userpass = $("#tuserpass").val();
	var userpass2 = $("#tuserpass2").val();
	var email = $("#temail").val();
	var yanzheng = $("#yanzheng").val();
	//1.1校验数据是否为空
	var isnull = true;
	isnull = isNull("tusername","登录名") && isnull;
	isnull = isNull("tuserpass","密码") && isnull;
	isnull = isNull("tuserpass2","确认密码") && isnull;
	isnull = isNull("temail","密码") && isnull;
	isnull = isNull("tyanzheng","验证码") && isnull;
	if(isnull == false){
		return false;
	}else{
		//1.2密码
		if(userpass != userpass2){
			layer.tips("密码不一致!", "#tuserpass2",{tipsMore: true,tips: [2, '#78BA32']});
			return false;
		}
		
		//2.校验验证码
		
		$.post("/dogstore/ajaxValidateCode2.action",
				{
					yanzheng:yanzheng
				},
				function(result){
					alert("regist");
					if(!result){
						//2-2.验证码错误，不提交
						layer.tips("验证码错误!","#tyanzheng",{tipsMore: true,tips: [2, '#78BA32']});
						return false;
					}else{
						var index = layer.load(2, {time: 10000}); //缓冲10秒
						//3.验证码正确，提交数据
						$.ajax({ 
							type:"post",
							url:"/dogstore/regist.action",
							async:false,  
							data:{
								username:username,
								email:email,
								userpass:userpass
							},
							success:function(error){
								if(error == "0"){
									//关闭缓冲
									layer.close(index);
									layer.msg("注册成功!",{icon: 1,shift: 2});
									setTimeout(function (){
										//关闭弹窗
										closeThis(); 
										//刷新
										window.location.reload();
									},1500);
								}else{
									layer.close(index);
									if(error == "1"){
										layer.msg("用户名已存在!",{icon: 2,shift: 6});
									}else if(error == "2"){
										layer.msg("数据添加失败!请联系prince!500",{icon: 2,shift: 6});
									}
									return false;
								}
							},error:function(){
								layer.close(index);
								layer.msg("注册提交失败!请联系prince..",{shift: 6});
								return false;
							}
						},"json");	
					}
				}
		);
	}
});

function isNull(name,error){
    var isnull = true;
    var value = $("#"+name).val().trim();
	if(value == "" || value == null){
		layer.tips(error+"不能为空!", "#"+name,{tipsMore: true,tips: [2, '#78BA32']});
		isnull = false;
	}
	return isnull;
}

function changeImg(img){
	img.src = img.src+"?time="+new Date().getTime();
}
</script>